package com.mxpio.erp.quality.service.impl;

import com.mxpio.erp.common.quality.entity.VEInspectionBillPassRate;
import com.mxpio.erp.common.quality.entity.VEInspectionBillUnqualifiedParts;
import com.mxpio.erp.common.quality.service.VEInspectionBillSupplierPassRateService;
import com.mxpio.erp.common.quality.service.VEInspectionBillUnqualifiedPartsService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.lin.Linq;
import com.mxpioframework.jpa.query.Criteria;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class VEInspectionBillUnqualifiedPartsServiceImpl implements VEInspectionBillUnqualifiedPartsService {

    @Override
    @Transactional(readOnly = true)
    public List<VEInspectionBillUnqualifiedParts> list(Criteria c) {
        return JpaUtil.linq(VEInspectionBillUnqualifiedParts.class).where(c).list();
    }

    @Override
    @Transactional(readOnly = true)
    public Page<VEInspectionBillUnqualifiedParts> listPage(Pageable pageAble, Criteria c) {
        return JpaUtil.linq(VEInspectionBillUnqualifiedParts.class).where(c).paging(pageAble);
    }

    @Override
    @Transactional(readOnly = true)
    public List<VEInspectionBillUnqualifiedParts> getlist(Criteria criteria) {
        Linq linq =JpaUtil.linq(VEInspectionBillUnqualifiedParts.class);
        return linq.aliasToBean(VEInspectionBillUnqualifiedParts.class).select("yearMonth","type",
                        linq.criteriaBuilder().sum(linq.root().get("quantity")).alias("quantity"),
                        linq.criteriaBuilder().sum(linq.root().get("lastMonth")).alias("lastMonth"),
                         linq.criteriaBuilder().sum(linq.root().get("lastYear")).alias("lastYear"))
                .where(criteria)
                .groupBy("yearMonth","type")
                .list();
    }
}
